package com.qfhd.soft.cjzcore.UI.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.qfhd.soft.cjzcore.R;
import com.qfhd.soft.cjzcore.UI.Listener.CJListener;

public class MessageDialog {
    private Context mContext;
    private Activity mActivity;
    private AlertDialog dialog;
    private TextView tx_title;
    private TextView tx_message;
    private Button closeBtn;
    private Button btn;
    private CJListener listener;
    private String btnText;

    public static MessageDialog dialogShow(Activity activity,Context context,String title, String message){
        MessageDialog messageDialog = new MessageDialog(context,activity);
        messageDialog.setTitle(title);
        messageDialog.setMessage(message);
        messageDialog.show();
        return messageDialog;
    }

    public static void dialogShow(Activity activity,Context context, String title, String message, CJListener listener){
        MessageDialog messageDialog = new MessageDialog(context,activity);
        messageDialog.setTitle(title);
        messageDialog.setMessage(message);
        messageDialog.listener = listener;
        messageDialog.show();
    }

    public static void dialogShow(Activity activity,Context context, String title, String message, String btnText,CJListener listener){
        MessageDialog messageDialog = new MessageDialog(context,activity);
        messageDialog.setTitle(title);
        messageDialog.setMessage(message);
        messageDialog.btn.setText(btnText);
        messageDialog.btn.setVisibility(View.VISIBLE);
        messageDialog.btn.setOnClickListener(v -> {
            if(listener != null){
                listener.finish(null);
            }
            messageDialog.dismiss();
        });
        messageDialog.show();
    }

    public MessageDialog(Context context, Activity mActivity) {
        mContext = context;
        mActivity = mActivity;
        init();
    }

    private void init() {
        View dialogView = LayoutInflater.from(mActivity).inflate(R.layout.message_dialog_layout, null);

        tx_title = dialogView.findViewById(R.id.message_dialog_title);
        tx_message = dialogView.findViewById(R.id.message_dialog_message);
        closeBtn = dialogView.findViewById(R.id.message_dialog_close);
        btn = dialogView.findViewById(R.id.message_dialog_btn);

        dialog = new AlertDialog.Builder(this.mActivity)
                .setCancelable(false)
                .setView(dialogView)
                .create();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        btn.setVisibility(View.GONE);//默认不显示

        closeBtn.setOnClickListener(v -> {
            dismiss();
            if(listener != null){
                listener.finish(null);
            }
        });
    }

    public void setTitle(String title){
        tx_title.setText(title);
    }

    public void setMessage(String message){
        tx_message.setText(message);
    }

    public void show(){
        if(!dialog.isShowing()){
            dialog.show();
        }
    }

    public void dismiss(){
        dialog.dismiss();
    }
}
